package com.angzh.bolibao.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class Customer extends Model<Customer>{
	
	public static final Customer dao = new Customer();
	//公司下客户列表
	public Page<Customer> paginate(int pageNum,int pageSize,String startTime,String endTime, int isCompensation,int role,String company){
		String sql1 = "";
		if(startTime!=null&&!startTime.equals("")){
			sql1 += " and buytime >= '"+startTime+" 00:00:00'";
		}
		if(endTime!=null && !endTime.equals("")){
			sql1 += " and buytime <= '"+endTime+" 23:59:59'";
		}
		if(isCompensation!=-1){
			sql1 += " and isCompensation ="+isCompensation;
		}
		return paginate(pageNum, pageSize, "select c.*", 
				"from customer c , user u where c.recommended = u.weichat "+
				" and u.company='"+company+"' and u.role=3"
						+sql1+" order by buytime");
		
	}
	//员工客户列表
	public Page<Customer> paginate(int pageNum,int pageSize,String weichat){
		return paginate(pageNum,pageSize,"select *","from customer where recommended='"+weichat+"'");
	}
	//佣金统计
	public List<Customer> totalCommission(String startTime,String endTime,String weichat,int type){
		if(weichat!=null && !weichat.equals("")){
			String sql = "";
			if(startTime!=null&&!startTime.equals("")){
				sql += " and buytime >= '"+startTime+" 00:00:00'";
			}
			if(endTime!=null && !endTime.equals("")){
				sql += " and buytime <= '"+endTime+" 23:59:59'";
			}
			if(weichat!=null && !weichat.equals("")){
				sql += " and recommended='"+weichat+"' and type="+type;//type是产品类型
			}
			List<Customer> c = this.find("select * from customer c where 1=1"+sql);
			return c;
		}else{
			
		}
		return null;
	}
	//通过车牌查找保单
	public List<Customer> getByLicense(String license,int comptype){
		return this.dao.find("select * from customer where license='"+license+"' and isCompensation="+comptype);
		
	}
	//查找赔付表单
	public Page<Customer> getCustomerList(int pageNum,int pageSize,String startTime,String endTime,String phone,int isCompensation){
		String sql = " where 1=1 ";
		if(isCompensation != -1){
			sql += " and isCompensation="+isCompensation;
		}
		if(startTime!=null&&!startTime.equals("")){
			sql += " and buytime >= '"+startTime+" 00:00:00'";
		}
		if(endTime!=null && !endTime.equals("")){
			sql += " and buytime <= '"+endTime+" 23:59:59'";
		}
		if(phone!=null && !phone.equals("")){
			sql += " and phone ='"+phone+"'";
		}
		return this.dao.paginate(pageNum, pageSize, "select *", "from customer "+sql +" order by comptime desc");
	}
	public List<Customer> getCustomerToList(String startTime,String endTime,String phone,int isCompensation){
		String sql = " where 1=1 ";
		if(isCompensation != -1){
			sql += " and isCompensation="+isCompensation;
		}
		if(startTime!=null&&!startTime.equals("")){
			sql += " and buytime >= '"+startTime+" 00:00:00'";
		}
		if(endTime!=null && !endTime.equals("")){
			sql += " and buytime <= '"+endTime+" 23:59:59'";
		}
		if(phone!=null && !phone.equals("")){
			sql += " and phone ='"+phone+"'";
		}
		return this.dao.find("select * from customer "+sql +" order by comptime desc");
	}
}
